package com.geek.beans.ioc;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyEditorSupport;
import java.util.stream.Stream;

/**
 *
 * Bean的元信息
 * @author : zjj
 * @date : 2021/12/27 16:37
 */
public class BeanInfoDemo {

    public static void main(String[] args) throws IntrospectionException {

        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class,Object.class);
        // 描述信息
        /**
         * propertyDescriptor 在spring 框架中运用的非常的广泛
         */
        Stream.of(beanInfo.getPropertyDescriptors()).forEach(
                propertyDescriptor ->{
                    // 每个propertyDescriptor 允许添加属性编辑器 propertyEditor 
                    // GUI 中，text(String)  -> propertyEditor
                    // String -> String
                    // String -> Integer
                    Class<?> propertyType = propertyDescriptor.getPropertyType();
                    String propertyTypeName = propertyType.getName();
                    if("name".equals(propertyTypeName)){
                        propertyDescriptor.setPropertyEditorClass(StringToIntegerPropertyEditor.class);

                    }



                    System.out.println(propertyDescriptor);
                }
        );
    }

    /**
     *
     */
    static class StringToIntegerPropertyEditor extends PropertyEditorSupport{
        /**
         *
         * @param text
         * @throws IllegalArgumentException
         */
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            super.setAsText(text);
            // 类型进行一下转换
            Integer value = Integer.valueOf(text);
            // 之后取 getValue 就是取出我们设置的值
            setValue(value);
        }
    }
}
